package com.yl.chainStore.services.impl.sick

import com.baomidou.mybatisplus.core.toolkit.Wrappers
import com.yl.chainStore.common.enums.TypeEnum
import com.yl.chainStore.entity.sick.SickMedicalHistory
import com.yl.chainStore.entity.user.CompanyUserOperationLog
import com.yl.chainStore.entity.user.CompanyUserOperationLogDetail
import com.yl.chainStore.mapper.pay.PayCustomerOrderMapper
import com.yl.chainStore.mapper.sick.SickCustomerImageMapper
import com.yl.chainStore.mapper.sick.SickMedicalHistoryMapper
import com.yl.chainStore.mapper.user.CompanyUserOperationLogDetailMapper
import com.yl.chainStore.mapper.user.CompanyUserOperationLogMapper
import com.yl.chainStore.pojo.vo.user.CompanyUserOperationLogSelectVo
import com.yl.chainStore.services.ICompanyUserOperationLogService
import com.yl.chainStore.services.sick.ISickMedicalHistoryService
import com.yl.chainStore.services.warehouse.IWarehouseCheckOrderService
import com.yl.common.ErrorCode
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnError
import com.yl.common.control.returnSuccess
import com.yl.common.getContext
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class SickMedicalHistoryServiceImpl(
	private val payCustomerOrderMapper: PayCustomerOrderMapper,
	private val sickCustomerImageMapper: SickCustomerImageMapper,
	private val sickMedicalHistoryMapper: SickMedicalHistoryMapper,
	private val companyUserOperationLogMapper: CompanyUserOperationLogMapper,
	private val companyUserOperationLogDetailMapper:CompanyUserOperationLogDetailMapper
) : ISickMedicalHistoryService {

	@Autowired
	lateinit var iCompanyUserOperationLogService: ICompanyUserOperationLogService

	companion object {
		private val log = LoggerFactory.getLogger(ISickMedicalHistoryService::class.java)
	}



	override fun bindImageToHistory(historyId: Long, imageId: Long): ResponseBody {
		log.info("病历管理 ==>病历绑定图片 ==>historyId:{},imageId:{} ",historyId,imageId)
		val image =
			sickCustomerImageMapper.selectById(imageId) ?: return returnError(ErrorCode.DATA_IS_NOT_FIND, "图片")
		image.medicalHistoryId = historyId
		sickCustomerImageMapper.updateById(image)
		return returnSuccess()
	}

	override fun updateMedicalHistory(medicalHistory: SickMedicalHistory): ResponseBody {
		log.info("病历管理 ==>修改病历 ==>medicalHistory:{} ", medicalHistory)
		val oldHistory = sickMedicalHistoryMapper.selectById(medicalHistory.id)
		val updateHistory = SickMedicalHistory(
			id = medicalHistory.id,
			createTime = oldHistory.createTime,
			createUserId = oldHistory.createUserId,
			customerId = oldHistory.customerId,
			type = medicalHistory.type,
			recordId = oldHistory.recordId,
			doctorId = medicalHistory.doctorId,
			nurseId = medicalHistory.nurseId,
			seeDoctorTime = medicalHistory.seeDoctorTime,
			labelIds = medicalHistory.labelIds,
			remark = medicalHistory.remark,
			actionChief = medicalHistory.actionChief
		)
		updateHistory.other = medicalHistory.other
		sickMedicalHistoryMapper.updateById(updateHistory)

		iCompanyUserOperationLogService.addLog(
			associatedId = medicalHistory.id,
			modelType = TypeEnum.COMPANY_USER_OPERATION_LOG_MODEL_TYPE_RECORD,
			tableType = TypeEnum.COMPANY_USER_OPERATION_LOG_TABLE_TYPE_MEDICAL_HISTORY,
			updateType = TypeEnum.COMPANY_USER_OPERATION_LOG_UPDATE_TYPE_UPDATE,
			beforeObj = oldHistory,
			afterObj = updateHistory,
			customerId = medicalHistory.customerId,
			createUserId = getContext().userId
		)
		return returnSuccess()
	}

	override fun delMedicalHistory(id: Long): ResponseBody {
		log.info("病历管理 ==>删除病历 ==>id:{} ", id)
		val oldHistory = sickMedicalHistoryMapper.selectById(id)
		sickMedicalHistoryMapper.deleteById(id)
		iCompanyUserOperationLogService.addLog(
			associatedId = id,
			modelType = TypeEnum.COMPANY_USER_OPERATION_LOG_MODEL_TYPE_RECORD,
			tableType = TypeEnum.COMPANY_USER_OPERATION_LOG_TABLE_TYPE_MEDICAL_HISTORY,
			updateType = TypeEnum.COMPANY_USER_OPERATION_LOG_UPDATE_TYPE_DEL,
			beforeObj = oldHistory,
			afterObj = null,
			customerId = oldHistory.customerId,
			createUserId = getContext().userId
		)
		return returnSuccess()
	}

	override fun getHistoryOperationLog(id: Long): List<CompanyUserOperationLog> {
		val selectLogList = companyUserOperationLogMapper.selectLogList(
			CompanyUserOperationLogSelectVo(
				associatedId = id,
				modelType = TypeEnum.COMPANY_USER_OPERATION_LOG_MODEL_TYPE_RECORD,
				tableType = TypeEnum.COMPANY_USER_OPERATION_LOG_TABLE_TYPE_MEDICAL_HISTORY,
			)
		)
		for(opLog in selectLogList){
			opLog.details = companyUserOperationLogDetailMapper.selectList(
				Wrappers.query<CompanyUserOperationLogDetail?>().eq(
					"log_id",opLog.id
				)
			)
		}

		return selectLogList
	}

	//	override fun complete(historyId: Long):ResponseBody{
//		val sickMedicalHistory =
//			sickMedicalHistoryMapper.selectById(historyId) ?: return returnError(ErrorCode.DATA_IS_NOT_FIND, "未创建病历")
//		sickMedicalHistory.type
//		sickMedicalHistoryMapper.updateById(sickMedicalHistory)
//		return returnSuccess()
//	}
}


